﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zero.Code;
using Zero.BLL;
using Zero.Model;

/*
 * 作者：edl
 * 功能：登录
 */

namespace Zero.Framework.Controllers.Login
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 生成验证码文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetAuthCode()
        {  
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="code">验证码</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CheckLogin(string username, string password, string code)
        {
            try
            {
                if (Session["nfine_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["nfine_session_verifycode"].ToString())
                {
                    
                    throw new Exception("验证码错误，请重新输入");
                }
                string data = new BLL.UserLogInfo().CheckLogin(username, password);
                var User= Zero.Code.Json.ToJObject(data);
                int LoginUserId = User.Root["data"]["L_UserId"].ToInt();//获取登录用户Id
                users userEntity = new UserLogInfo().GetUserInfo(LoginUserId);//获取登陆用户信息
                OperatorModel operatorModel = new OperatorModel();
                if (userEntity != null)
                {
                    operatorModel.UserId = userEntity.U_Id.ToString();
                    operatorModel.UserCode = userEntity.U_Account;
                    operatorModel.UserName = userEntity.U_RealName;
                    operatorModel.CompanyId = userEntity.U_OrganizeId.ToString();
                    operatorModel.DepartmentId = userEntity.U_DepartmentId.ToString();
                    operatorModel.RoleId = userEntity.U_RoleId.ToString();
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    if (userEntity.U_Account == "admin")
                    {
                        operatorModel.IsSystem = true;
                    }
                    else
                    {
                        operatorModel.IsSystem = false;
                    }
                    new OperatorProvider().AddCurrentOperator(operatorModel);
                }
                    
                
                return Content(data);
            }
            catch (Exception ex)
            {
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }
    }
}